//
//  HLNewfeatureController.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/3/1.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

/*

如果把属性定义在类的外面那么外部就也可以访问  

加了private修饰那么就代表只能在当前这个页面可以访问 （包括别的类）

*/
private let reuseIdentifier = "Cell"

private let itemCount = 4

class HLNewfeatureController: UICollectionViewController {

    
    init() {
        let flowLayout = UICollectionViewFlowLayout()
        super.init(collectionViewLayout: flowLayout)
        
        flowLayout.itemSize = self.collectionView!.bounds.size
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        self.collectionView?.bounces = false
        self.collectionView?.pagingEnabled = true
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.registerClass(HLNewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        collectionView?.delegate = self
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HLNewfeatureCell
        
        cell.index = indexPath.item
        
        return cell
        
    }
}

// MARK: - UICollectionViewDelegate
extension HLNewfeatureController {
    
    /// 拖拽动画结束时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
       let currentIndex = Int(collectionView!.contentOffset.x / collectionView!.bounds.width)
        if currentIndex == itemCount - 1{
            
            let indexPath = NSIndexPath(forItem: currentIndex, inSection: 0)
            
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HLNewfeatureCell
            
            //通过一个动画来显示按钮
            cell.showStarButton()
        }
    }
    
    
}

/// 统一文件中可以定义多个类
class HLNewfeatureCell:UICollectionViewCell {
    
    //默认让它选中第0个
    var index:Int = 0{
    //当给index赋值完成后调用
    didSet {
        imageView.image = UIImage(named: "new_feature_\(index + 1)")
        starButton.hidden = true
        }
    }
    
    lazy var imageView = UIImageView()
    
    lazy var starButton:UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.sizeToFit()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "chooseController", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    /// 点击开始体验时切换控制器的根控制器
    func chooseController() {
        UIApplication.sharedApplication().keyWindow?.rootViewController = HLWolcomeViewController()
    }
    
    /// 显示开始按钮动画
    func showStarButton() {
        
        starButton.hidden = false
        starButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
//            self.starButton.transform = CGAffineTransformMakeScale(1, 1)
            self.starButton.transform = CGAffineTransformIdentity
            
            
            }) { (_) -> Void in
                
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /// 设置ui相关的事件
    func prepareUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(starButton)
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        starButton.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[imageView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["imageView":imageView]))
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[imageView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["imageView":imageView]))
        
        
        contentView.addConstraint(NSLayoutConstraint(item: starButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
         contentView.addConstraint(NSLayoutConstraint(item: starButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    
}






